/*
 * Copyright (c) 2023. Shanghai HEADING information Engineering Co., Ltd. All rights reserved.
 */

package com.lovely602.boot.bootdemo.controller.java;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lizhixing
 */
@Slf4j
public class SynchronizedTest {

  volatile int a = 1;
  volatile int b = 1;

  public synchronized void add() {
    log.info("add start");
    for (int i = 0; i < 10000; i++) {
      a++;
      b++;
    }
    log.info("add done");
  }

  public synchronized void compare() {
    log.info("compare start");
    for (int i = 0; i < 10000; i++) {
      // a始终等于b吗？
      if (a < b) {
        log.info("a:{},b:{},{}", a, b, a > b);
        // 最后的a>b应该始终是false吗？
      }
    }
    log.info("compare done");
  }

  public static void main(String[] args) {
    SynchronizedTest synchronizedTest = new SynchronizedTest();
    new Thread(synchronizedTest::add).start();
    new Thread(synchronizedTest::compare).start();
  }

}
